import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:jinke/page/pro_list/main/widgets/cell.dart';
import 'package:jinke/page/pro_list/model/pro_filter_model.dart';
import 'package:jinke/page/pro_list/model/pro_list_model.dart';
import 'package:jinke/utils/component/dialog/drop.dart';
import 'package:jinke/utils/component/filter_view/model/filter_model.dart';
import 'package:jinke/utils/component/list/section.dart';
import 'package:jinke/utils/httputils/api.dart';
import 'package:jinke/utils/httputils/resultdata.dart';
import 'package:jinke/utils/util.dart';

import 'index.dart';

class ProListController extends GetxController
    with GetSingleTickerProviderStateMixin {
  ProListController();

  final state = ProListState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.tabCtrl = TabController(length: 3, vsync: this, initialIndex: 1);
    state.tabCtrl.addListener(() {
      if (state.tabCtrl.index != state.tabCtrl.previousIndex &&
          !state.tabCtrl.indexIsChanging) {
        getProList();
      }
    });
    state.tabTitles =
        ['我负责的', '我参与的', '全局项目'].map((e) => TabModel(name: e)).toList();
    state.filterList = ['排序', '投资金额']
        .asMap()
        .map((key, value) => MapEntry(
            key,
            FilterModel(
              title: value,
              values: [],
              selectedIndex: 0,
              emptyOnTap: (p0) {
                return getFilterTypes(p0);
              },
              onTap: ((p0) {
                var mod = state.filterList[key];
                mod.selectedIndex = p0;
                if (key == 0) {
                  state.sortMod = state.sortList[p0];
                } else {
                  state.moneyMod = state.moneyList[p0];
                }
                getProList();
              }),
            )))
        .values
        .toList();
    getProList();
  }

  void getProList() {
    bool isMine = state.tabCtrl.index <= 1;
    String toParticipate = state.tabCtrl.index == 1 ? '1' : '';

    ProListApi.postProList(
            isMine: isMine,
            toParticipate: toParticipate,
            order: state.sortMod?.code,
            money: state.moneyMod?.code)
        .then((res) {
      if (res.isSuccess) {
        List data = res.data['content'];
        setupData(data);
      }
    });
  }

  void setupData(List data) {
    var items = data.map((e) => ProListModel.fromJson(e)).toList();
    var sec = SectionModel(
        index: 0,
        itemBuilder: ((context, index) {
          return ProListCell(
            mod: items[index],
            index: index,
          );
        }),
        items: items);
    state.list = [sec];
    state.tabTitles[state.tabCtrl.index].count = items.length;
    update();
  }

  Future<List<String>> getFilterTypes(int index) async {
    ResultData res = await ProListApi.getFilters(
        codeType: index == 0 ? 'APP_ORDER' : 'MONEY_SCREENING');
    if (res.isSuccess) {
      List data = res.data;
      List<ProFilterModel> list =
          data.map((e) => ProFilterModel.fromJson(e)).toList();
      if (index == 0) {
        state.sortList = list;
      } else {
        state.moneyList = list;
      }
      return list.map((e) => e.name ?? '').toList();
    }
    return [''];
  }

  void onTap(int index) {}

  void actionCell(ProListModel mod) {
    Get.toNamed(RouterPath.pro_detail, arguments: mod);
  }

  void focus(ProListModel mod) async {
    ResultData res = await ProListApi.focus(id: mod.id ?? '');
    if (res.isSuccess) {
      EasyLoading.showSuccess(mod.isFocus == 1 ? '取消成功' : '关注成功');
      mod.isFocus = mod.isFocus == 1 ? 0 : 1;
      update();
    } else {
      EasyLoading.showError('操作失败');
    }
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
